package com.transformer.shobal.activitys.canvas;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2015/2/5.
 */
public class CanvasDemoView extends View {
    private Paint mPaint = new Paint();
    public CanvasDemoView(Context context) {
        super(context);
        init();
    }

    public CanvasDemoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CanvasDemoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }



    private void init(){

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawNoSave(canvas);
        drawBg(canvas);
        drawDiff(canvas);
        drawReplace(canvas);
        drawUnion(canvas);
        drawXor(canvas);
        drawReverse(canvas);
        drawIntersect(canvas);
        drawReplace1(canvas);
    }

    private void drawNoSave(Canvas canvas){
        canvas.save();
        canvas.rotate(45,50,50);
        mPaint.setColor(Color.RED);
        canvas.drawRect(0,0,100,100,mPaint);
        canvas.restore();
//        canvas.drawRect(600,0,700,100,mPaint);
    }

    //背景
    private void drawBg(Canvas canvas){
        canvas.save();//平移、放缩、旋转、错切、裁剪等操作 针对save以后restore之前的绘制有效
        canvas.translate(10,200);
        drawScene(canvas);
        canvas.restore();
    }

    //相减
    private void drawDiff(Canvas canvas){
        canvas.save();//平移、放缩、旋转、错切、裁剪等操作 针对save以后restore之前的绘制有效
        canvas.translate(10 + 300, 200);
        canvas.clipRect(0,0,100,100);
        canvas.clipRect(40,40,200,200,Region.Op.DIFFERENCE);
        drawScene(canvas);
        canvas.restore();
    }

    //取代
    private void drawReplace(Canvas canvas){
        Path path = new Path();
        canvas.save();
        canvas.translate(10+300+300, 200);
        path.reset();
        canvas.clipPath(path); // makes the clip empty
        path.addCircle(50, 50, 50, Path.Direction.CCW);
        canvas.clipPath(path, Region.Op.REPLACE);
        drawScene(canvas);
        canvas.restore();

        canvas.save();
        canvas.translate(10+300+300, 200);
        canvas.clipRect(0, 0, 100, 100);
        canvas.clipRect(40, 40, 150, 150, Region.Op.REPLACE);
        drawScene(canvas);
        canvas.restore();
    }

    //并集
    private void drawUnion(Canvas canvas){
        canvas.save();
        canvas.translate(10+300+300+300, 200);
        canvas.clipRect(0, 0, 60, 60);
        canvas.clipRect(40, 40, 100, 100, Region.Op.UNION);
        drawScene(canvas);
        canvas.restore();
    }

    //存异去同XOR
    private void drawXor(Canvas canvas){
        canvas.save();
        canvas.translate(10, 500);
        canvas.clipRect(0, 0, 60, 60);
        canvas.clipRect(40, 40, 100, 100, Region.Op.XOR);
        drawScene(canvas);
        canvas.restore();
    }

    //Difference的相反运算，将后画的部分中去掉先前的部分
    private void drawReverse(Canvas canvas){
        canvas.save();
        canvas.translate(10+300, 500);
        canvas.clipRect(0, 0, 60, 60);
        canvas.clipRect(40, 40, 100, 100,
                Region.Op.REVERSE_DIFFERENCE);
        drawScene(canvas);
        canvas.restore();

//      canvas.save();
//      canvas.translate(160, 310);
//      canvas.clipRect(0, 0, 60, 60);
//      canvas.clipRect(40, 40, 100, 100,
//      Region.Op.INTERSECT);
//      drawScene(canvas);
//      canvas.restore();
    }

    //交集
    private void drawIntersect(Canvas canvas){
        canvas.save();
        canvas.translate(10+300+300, 500);
        canvas.clipRect(0, 0, 60, 60);
        canvas.clipRect(40, 40, 100, 100,
                Region.Op.INTERSECT);
        drawScene(canvas);
        canvas.restore();
    }

    //取代
    private void drawReplace1(Canvas canvas){
        Path path = new Path();
        canvas.save();
        canvas.translate(10+300+300+200, 500);
        path.reset();
        canvas.clipPath(path); // makes the clip empty
        path.addCircle(100, 100, 100, Path.Direction.CCW);
        canvas.clipPath(path, Region.Op.REPLACE);
        drawScene(canvas);
        canvas.restore();
    }

    //背景
    private void drawScene(Canvas canvas) {
        canvas.clipRect(0, 0, 200, 200);

        canvas.drawColor(Color.WHITE);

        mPaint.setColor(Color.RED);
        canvas.drawLine(0, 0, 200, 200, mPaint);

        mPaint.setColor(Color.GREEN);
        canvas.drawCircle(60, 140, 60, mPaint);

        mPaint.setColor(Color.BLUE);
        canvas.drawText("Clipping", 100, 100, mPaint);
    }
}
